<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <button class="connect">连接服务端</button>
  <button class="send" disabled="true">发送数据给服务端</button>
  从服务器接收的数据如下：<span class="receive"></span>
  <script>
    // window对象提供了webscoket，不需要下载额外的包
    var connect = document.querySelector('.connect')
    var send = document.querySelector('.send')
    var receive = document.querySelector('.receive')
    let ws = null
    connect.onclick = function() {
      ws = new WebSocket('ws://localhost:9998')
      ws.onopen = () => {
        console.log('连接成功')
        send.disabled = false
      }
      ws.onclose = () => {
        console.log('连接失败')
        send.disabled = true
      }
      ws.onmessage = msg => {
        console.log('接收到从服务端发送的数据')
        console.log(msg)
        receive.innerHTML = msg.data
      }
    }
    send.onclick = function() {
      // 前端发送数据后端
      ws.send(JSON.stringify({
        action: 'fullScreen',
        socketType: 'fullScreen',
        chartName: 'trend',
        value: true
      }))
    }
  </script>
</body>
</html>